package com.user.seat.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.user.seat.controller.WebSocketServer;
import com.user.seat.dao.SeatDao;
import com.user.seat.domain.*;
import com.user.seat.service.SeatService;
import com.user.seat.utils.JwtService;
import io.jsonwebtoken.Jwt;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

/**
 * @author jimmy
 * Date  2022/4/22 14:25
 * @version 1.0
 */
@Service
public class SeatServiceImpl implements SeatService {

    private SeatDao seatDao;
    public SeatServiceImpl(SeatDao seatDao){
        this.seatDao = seatDao;

    }


    @Override
    public ResultInfo setSeatsNumber(SeatNumber seatNumber) {
        ResultInfo resultInfo = new ResultInfo();

        seatDao.setSeatsNumber(seatNumber);
        resultInfo.setMsg("成功");
        resultInfo.setCode(201);
        return resultInfo;
    }

    @Override
    public ResultInfo getSeatsNumber() {
        ResultInfo resultInfo = new ResultInfo();

        Map<String,Integer> map = new HashMap<>();
        map.put("seatNumber",seatDao.getSeatsNumber());
        resultInfo.setMsg("成功");
        resultInfo.setCode(201);
        resultInfo.setBody(map);
        return resultInfo;
    }

    @Override
    public ResultInfo doTakeSeat(SeatStatusInfo seatStatusInfo) {
        ResultInfo resultInfo = new ResultInfo();
        List<Integer> resultNumber = new ArrayList<>();
        List<SeatStatus> seatStatusList = seatStatusInfo.getSeatStatusList();
        if (seatStatusList.size()>0){
            seatStatusList.forEach(seatStatus -> {
                Integer number = seatStatus.getSeatNumber();
                if (ObjectUtils.isNotEmpty(number)){
                    int id = seatDao.insertSeat(number,seatStatus);
                    if (id >0) {
                        try {
                            JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(seatStatusInfo));
                            json.put("isAdd",true);
                            WebSocketServer.sendInfo(JSONObject.toJSONString(json),seatStatus.getUserId()+"");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        resultNumber.add(number);
                    }

                    resultInfo.setMsg("成功");

                }else {
                    resultInfo.setMsg("无抢座号码");
                }
                resultInfo.setBody(resultNumber);
                resultInfo.setCode(200);
            });
        }
        return resultInfo;
    }

    @Override
    public ResultInfo doGetSeat() {
        ResultInfo resultInfo = new ResultInfo();

        List<SeatStatus> seatStatusList = seatDao.getSeat();
        resultInfo.setBody(seatStatusList);
        resultInfo.setCode(200);
        resultInfo.setMsg("成功");
        return resultInfo;
    }

    @Override
    public ResultInfo doResetSeat() {
        seatDao.doResetSeat();
        return null;
    }

    @Override
    public ResultInfo doOutSeat(SeatStatus seatStatus) {
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setCode(201);
        resultInfo.setMsg("成功");
        int count = seatDao.updateSeatByUserId(seatStatus);
        if (count>0){
            resultInfo.setBody(count);
            JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(seatStatus));
            json.put("isAdd",false);
            try {
                WebSocketServer.sendInfo(JSONObject.toJSONString(json),seatStatus.getUserId()+"");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultInfo;
    }
}
